package cn.kgc.fxy.smbms.web;

import cn.kgc.fxy.smbms.entity.Bill;
import cn.kgc.fxy.smbms.entity.Provider;
import cn.kgc.fxy.smbms.service.BillService;
import cn.kgc.fxy.smbms.service.ProviderService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {
    @Resource
    private ProviderService providerService;
    @Resource
    private BillService billService;
    @RequestMapping("/show")
    public String show(Bill condition, Integer pageNum, Model model){
        Integer pageSize = 5;
        PageInfo<Bill> billPageInfo = this.billService.findBill(condition,pageNum,pageSize);
        List<Provider> providerList = this.providerService.findByproName();
        model.addAttribute("providerList",providerList);
        model.addAttribute("pageInfo",billPageInfo);
        return "bill/list";
    }
    @RequestMapping("/view")
    public String view(Long billId,Model model){
        Bill bill = this.billService.findView(billId);
        model.addAttribute("bill",bill);
        return "bill/view";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<Provider> providerList = this.providerService.findByproName();
        model.addAttribute("providerList",providerList);
        return "bill/add";
    }
    @RequestMapping("/doAdd")
    public String doAdd(@Valid Bill bill, BindingResult bindingResult, HttpSession session,Model model){
        if (bindingResult.hasErrors()){
            List<Provider> providerList = this.providerService.findByproName();
            model.addAttribute("providerList",providerList);
            return "bill/add";
        }
//        bill.setCreatedBy((User) session.getAttribute(Constant.USER));
        this.billService.addBill(bill);
        return "redirect:show";
    }
    @RequestMapping("/showBillCount")
    @ResponseBody
    public String showBillCount(String billCode){
        Integer billCount = this.billService.findBillCount(billCode);
        return billCount.toString();
    }
    @RequestMapping("/toModify")
    public String toModify(Model model,Long billId){
        List<Provider> providerList = this.providerService.findByproName();
        model.addAttribute("providerList",providerList);
        Bill bill = billService.findView(billId);
        model.addAttribute("bill",bill);
        return "bill/modify";
    }
    @RequestMapping("/doModify")
    public String doModify(@Valid Bill bill,BindingResult bindingResult,Model model){
        if (bindingResult.hasErrors()){
            List<Provider> providerList = this.providerService.findByproName();
            model.addAttribute("providerList",providerList);
            return "bill/add";
        }
        this.billService.modifyBill(bill);
        return "redirect:show";
    }

    @RequestMapping("/toRemove")
    public String toRemove(Long billId){
        billService.removeBill(billId);
        return "redirect:show";
    }
}
